package cn.eaay.freemarkerdemo.controller;

import cn.eaay.freemarkerdemo.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
public class HelloController {

    @GetMapping("basic")
    public String hello(Model model) {
        model.addAttribute("name", "James");
        Student student = Student.builder().name("勒布朗").age(37).build();
        model.addAttribute("student", student);
        return "basic";
    }

    @GetMapping("list")
    public ModelAndView list(ModelAndView modelAndView) {
        Student stu1 = Student.builder().name("刘备").age(36).money(100.89f).build();
        Student stu2 = Student.builder().name("关羽").age(35).money(100.78f).build();
        Student stu3 = Student.builder().name("张飞").age(34).money(19.89f).build();
        List<Student> studentList = Arrays.asList(stu1, stu2, stu3);
        modelAndView.addObject("students", studentList);

        Map<String, Student> stuMap = new HashMap<>();
        stuMap.put("stu1", stu1);
        stuMap.put("stu2", stu2);
        stuMap.put("stu3", stu3);
        modelAndView.addObject("stuMap", stuMap);

        modelAndView.setViewName("list");
        return modelAndView;
    }

    @GetMapping("test")
    public String test(Model model) {
        Student stu1 = Student.builder().name("小明").age(18).money(100F).build();
        Student stu2 = Student.builder().name("小红").age(15).money(90F).build();
        List<Student> students = Arrays.asList(stu1, stu2);
        model.addAttribute("students", students);
        return "instruction";
    }

    @GetMapping("compare")
    public String compare(Model model) {
        Date now = new Date();
        model.addAttribute("date1", now);
        model.addAttribute("date2", now);
        return "compare";
    }

    @GetMapping("fun")
    public String testFun(Model model) {
        List<String> names = Arrays.asList("周杰伦", "刘德华", "张学友", "岳云鹏");
        model.addAttribute("names", names);
        Date today = new Date();
        model.addAttribute("today", today);
        int point = 123456789;
        model.addAttribute("point", point);
        return "function";
    }
}
